﻿using System;
using BusinessObjects;
using LogInModule.Models;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common;
using YourWorld.Common.Infrastucture.Events;
using LogInModule.Views;

namespace LogInModule.ViewModels
{
    public class GameInitViewModel : ViewModelBase
    {
        private ILoginModel model;
        private IRegionManager regionManager;
        public IUnityContainer container;

        private GameLevelInfo gameLevelInfo = new GameLevelInfo();
        private Player player = new Player();

        public GameLevelInfo GameLevelInfo
        {
            get
            {
                return this.gameLevelInfo;
            }
        }

        public Player Player
        {
            get
            {
                return this.player;
            }
            set
            {
                if ( this.player != value )
                {
                    this.player = value;
                    this.NotifyPropertyChanged( "Player" );
                }
            }
        }

        public GameMode GameMode
        {
            get;
            set;
        }

        public CharacterTypes CharacterType
        {
            get;
            set;
        }

        public string Place
        {
            get;
            set;
        }

        public GameInitViewModel( ILoginModel model, IRegionManager regionManager, IUnityContainer container )
        {
            this.model = model;
            this.regionManager = regionManager;
            this.container = container;

            EventService.Aggregator.GetEvent<LogoutEvent>().Subscribe( this.OnPlayerLogout );
            EventService.Aggregator.GetEvent<PlayerLoginSuccessfulEvent>().Subscribe( this.OnPlayerLogIn );
        }

        public void OnPlayerLogout( object o )
        {
            if ( this.regionManager.Regions[RegionNames.HostRegion].Views.Contains( this.container.Resolve<LoginMainView>() ) )
                this.regionManager.Regions[RegionNames.HostRegion].Activate( this.container.Resolve<LoginMainView>() );

            if ( this.regionManager.Regions[RegionNames.LoginContent].Views.Contains( this.container.Resolve<GameInit>() ) )
                this.regionManager.Regions[RegionNames.LoginContent].Activate( this.container.Resolve<GameInit>() );

            this.container.Resolve<GameInit>().Restart();
        }

        public void OnPlayerLogIn( Player player )
        {
            this.Player = player;
        }

        public void OnPlayGame()
        {
            this.GameLevelInfo.Place = ( PlacesEnumeration )Enum.Parse( typeof( PlacesEnumeration ), this.Place, true );
            if ( this.GameMode == BusinessObjects.GameMode.SinglePlayer )
            {
                this.StartGameSinglePlayer();
            }
            else
            {
                this.StartGameMultiPlayer();
            }
        }

        private void StartGameMultiPlayer()
        {
            //TODO - add maps to the DB and get their ids here!!!!
            this.Player.MapId = this.GameLevelInfo.Place.ToString().Length;
            EventService.Aggregator.GetEvent<StartGameMultiPlayerEvent>().Publish( this.Player );
        }

        private void StartGameSinglePlayer()
        {
            //TODO - add maps to the DB and get their ids here!!!!
            this.Player.MapId = this.GameLevelInfo.Place.ToString().Length;
            EventService.Aggregator.GetEvent<StartGameSinglePlayerEvent>().Publish( this.Player );
        }
    }
}